package fr.demo.list;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;

public class BindingCustomList extends ListActivity implements OnItemClickListener {
	/**
	 * Adapter of the ListView
	 *  TAKECARE : The simple adapter has no operation for manage data in the list view.
	 *  You cannot ADD and DELETE element, but you can modify it.
	 *  IF YOU WANT TO ADD and DELETE element with this adapter, you must EXTEND this class
	 *  and put your operation for ADD and DELETE element.
	 */
	private SimpleAdapter adapter;

	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        /* Populate list */
        List<Map<String,String>> data = new ArrayList<Map<String,String>>();
        for(int i = 0;i<20;i++){
        	Map<String,String> ds = new HashMap<String,String>();
        	ds.put("property_1", "Property 1 E"+i);
        	ds.put("property_2", "INDEX is "+i);
        	data.add(ds);
        }
        
        /* Instanciate adapter with XML Layout representing on Item */
        adapter = new SimpleAdapter(
        		this,
        		data,
        		R.layout.item_cell_2,
        		new String[] { "property_1","property_2" },
        		new int[] { R.id.TextView01, R.id.TextView02 } );
        getListView().setAdapter(adapter);
        
        /* Bind the Click event */
        getListView().setOnItemClickListener(this);
        
        /* Bind Context Menu */
        getListView().setOnCreateContextMenuListener(this);
    }

	@SuppressWarnings("unchecked")
	public void onItemClick(AdapterView<?> ad, View cell_view, int position, long id) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(BindingCustomList.this,Information.class);
		Map<String,String> obj = (Map<String,String>) adapter.getItem(position);
		intent.putExtra("LABEL", obj.get("property_1"));
		startActivity(intent);
	}
	
}